﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.Public.ExcelToView
{
	internal class ViewMaker
	{
		internal ViewMaker(Autodesk.Revit.DB.Document m_doc)
		{
			this.m_doc = m_doc;
			this.prepareLst();
		}

		internal List<bool> MakeAllViewSheet(List<List<string>> input, List<int> TitleRank)
		{
			return new List<bool>();
		}

		private void prepareLst()
		{
			this.nowLstViewSheets.Clear();
			this.nowListTitleBlockes.Clear();
			this.nowListSheets.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FamilySymbol));
			filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				this.nowListTitleBlockes.Add(element as FamilySymbol);
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc);
			filteredElementCollector2.OfClass(typeof(ViewSheet));
			foreach (Element element2 in filteredElementCollector2.ToElements())
			{
				this.nowLstViewSheets.Add(element2 as ViewSheet);
				this.SheetNums.Add((element2 as ViewSheet).SheetNumber);
			}
			FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(this.m_doc);
			filteredElementCollector3.OfClass(typeof(FamilyInstance));
			filteredElementCollector3.OfCategory(BuiltInCategory.OST_TitleBlocks);
			foreach (Element element3 in filteredElementCollector3.ToElements())
			{
				this.nowListSheets.Add(element3 as FamilyInstance);
			}
		}

		public ViewSheet makeAViewSheet(List<string> lstInput, out string message)
		{
			int num = 1;
			bool flag = false;
			string text = lstInput[0];
			message = "";
			foreach (string text2 in this.SheetNums)
			{
				string a;
				if (text2.Last<char>() == ')')
				{
					int num2 = text2.Length - 1;
					a = text2.Substring(0, num2 - 2);
				}
				else
				{
					a = text2;
				}
				if (a == text)
				{
					flag = true;
					num++;
				}
			}
			this.SheetNums.Add(text);
			if (flag)
			{
				text = text + "(" + num.ToString() + ")";
			}
			string text3 = lstInput[1];
			ViewSheet viewSheet = null;
			Transaction transaction = new Transaction(this.m_doc, "addView");
			try
			{
				transaction.Start();
				viewSheet = ViewSheet.Create(this.m_doc, new ElementId(-1));
				viewSheet.SheetNumber = text;
				viewSheet.Name = text3;
				Autodesk.Revit.DB.View view = viewSheet;
				RevitVersionFuncs.SetViewName(ref view, text3);
				viewSheet = (view as ViewSheet);
				transaction.Commit();
			}
			catch (Exception)
			{
				message = "Revit不支持视图命名中存在特殊字符!";
				transaction.RollBack();
				return null;
			}
			return viewSheet;
		}

		private FamilyInstance searchNowAddedSheet()
		{
			FamilyInstance familyInstance = null;
			List<FamilyInstance> list = this.searchNowFamilyInsatance();
			bool flag = false;
			if (list == null || this.nowListSheets == null)
			{
				return null;
			}
			foreach (FamilyInstance familyInstance2 in list)
			{
				flag = false;
				foreach (FamilyInstance familyInstance3 in this.nowListSheets)
				{
					if (familyInstance2.Id.IntegerValue == familyInstance3.Id.IntegerValue)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					familyInstance = familyInstance2;
					break;
				}
			}
			this.nowListSheets.Add(familyInstance);
			return familyInstance;
		}

		private List<FamilyInstance> searchNowFamilyInsatance()
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			new FilteredElementCollector(this.m_doc);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FamilyInstance));
			filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				list.Add(element as FamilyInstance);
			}
			return list;
		}

		private ElementId searchTitleBlockesID(List<string> lstInput, List<int> TitleRank)
		{
			ElementId result = null;
			string text = Utility.GetNoneWhiteString(lstInput[TitleRank[2]]);
			text = text.Replace("+", "\\+");
			string pattern = "^" + text + "$";
			foreach (FamilySymbol familySymbol in this.nowListTitleBlockes)
			{
				bool flag = false;
				string familyName = familySymbol.FamilyName;
				foreach (string pattern2 in StaticResources.HyTitleBlockNameBase)
				{
					if (Regex.IsMatch(familyName, pattern2))
					{
						flag = true;
						break;
					}
				}
				if (flag && Regex.IsMatch(familySymbol.Name, pattern))
				{
					result = familySymbol.Id;
				}
			}
			return result;
		}

		private Document m_doc;

		private ExternalCommandData m_CommandData;

		private List<ViewSheet> nowLstViewSheets = new List<ViewSheet>();

		private List<string> SheetNums = new List<string>();

		private List<FamilySymbol> nowListTitleBlockes = new List<FamilySymbol>();

		private List<FamilyInstance> nowListSheets = new List<FamilyInstance>();
	}
}
